package com.knowledge.review.designPatterns.behavioralpattern.chainofresponsibilitypattern.demo5;

/**
 * @author XFW
 * @date 2024/5/9 15:16
 * @Description 创建AbstractHandler类
 **/
public abstract class AbstractHandler<T> {


    protected AbstractHandler nextHandler;

    protected void next(AbstractHandler handler){
        this.nextHandler=handler;
    }

    protected abstract void doHandler(T data);
	// Builder静态类
    public static class Builder<T>{

        private AbstractHandler<T> head;
        private AbstractHandler<T> tail;

        public Builder<T> addHandler(AbstractHandler<T> handler){
            if(this.head==null){
                this.head=this.tail=handler;
                return this;
            }
            this.tail.next(handler);
            this.tail=handler;
            return this;
        }

        public AbstractHandler<T> build(){
            // 添加默认的流程结尾
            AbstractHandler baseHandler=new BaseHandler();
            if(this.tail!=null){
                this.tail.next(baseHandler);
                this.tail=baseHandler;
            }
            if(this.head==null){
                this.head=baseHandler;
            }
            return this.head;
        }


    }
}
